﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SIA.HwAccess;

namespace SIA.AutomationControl
{
    public partial class NumericEdit : UserControl
    {
        IHwSignal _setSignal;
        IHwSignal _actSignal;

        [CategoryAttribute("SIA.AutomationControl"), DefaultValue("unit")]
        public String Unit { get { return lbUnit.Text; } set { lbUnit.Text = value; } }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue("Text")]
        new public String Text { get { return lbNome.Text; } set { lbNome.Text = value; } }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue("Text")]
        public String Comment { get { return lbNome.Text; } set { lbNome.Text = value; } }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue(true)]
        public Boolean UnitVisible { get { return lbUnit.Visible; } set { lbUnit.Visible = value; } }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue(true)]
        public Boolean actSignalVisible { get { return lbActValue.Visible; } set { lbActValue.Visible = value; } }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue(null)]
        public IHwSignal SetSignal
        {
            get { return _setSignal; }
            set
            {
                if (_setSignal != value)
                {
                    if (_setSignal != null) _setSignal.ValueChange -= new SignalEvent(_setSignal_ValueChange);
                    _setSignal = value;
                    tbInput.Text = _setSignal.Value.ToString();
                    if (_setSignal != null) _setSignal.ValueChange += new SignalEvent(_setSignal_ValueChange);
                }
            }
        }
        [CategoryAttribute("SIA.AutomationControl"), DefaultValue(null)]
        public IHwSignal ActSignal
        {
            get { return _actSignal; }
            set
            {
                if (_actSignal != value)
                {
                    if (_actSignal != null) _actSignal.ValueChange -= new SignalEvent(_actSignal_ValueChange);
                    _actSignal = value;
                    lbActValue.Text = _actSignal.Value.ToString();
                    if (_actSignal != null) _actSignal.ValueChange += new SignalEvent(_actSignal_ValueChange);
                }
            } 
        }


        public NumericEdit()
        {
            InitializeComponent();
            if (_actSignal != null)
            {
                _actSignal.ValueChange += new SignalEvent(_actSignal_ValueChange);
                this.lbActValue.Text = _actSignal.Value.ToString();
            }
            if (_setSignal != null)
            {
                _setSignal.ValueChange += new SignalEvent(_setSignal_ValueChange);
                this.tbInput.Text = _setSignal.Value.ToString();
            }
            tbInput.TextChanged += new EventHandler(tbInput_TextChanged);
        }

        void tbInput_TextChanged(object sender, EventArgs e)
        {
            int input = 0;
            if (int.TryParse(tbInput.Text, out input))
            {
                // Numero ok
                tbInput.ForeColor = Color.Black;
                _setSignal.Value = input;
            }
            else
            {    // Numero non intero
                tbInput.ForeColor = Color.Red;
            }   
        }
        void _actSignal_ValueChange(IHwSignal signal)
        {
            if (this.IsHandleCreated)
            this.Invoke(new MethodInvoker(delegate{
                this.lbActValue.Text = signal.Value.ToString();
            }));
        }
        void _setSignal_ValueChange(IHwSignal signal)
        {
            if (this.IsHandleCreated)
            this.Invoke(new MethodInvoker(delegate
            {
                this.tbInput.Text = signal.Value.ToString();
            }));
        }
    }
}
